#include "../WordFreq.h"
#include <stdio.h>
#include <string>
#include <string.h>
#include <fstream>
#include <list>
using namespace std;

/*
*/
void Display(const list<CWordFreq>& wflist)
{
    for (list<CWordFreq>::const_iterator it=wflist.begin(); it!= wflist.end(); it++)
    {
        cout<<(*it);
    }
}
/*
表回文判读
*/
bool isPalindrome(const list<char>& alist)
{
    list<char> copylist = alist;
    while (copylist.size() > 1)
    {
        if(copylist.front() != copylist.back())
        {
            return false;
        }
        copylist.pop_back();
        copylist.pop_front();
    }
    return true;
}

//有序表插入新元素，仍保持顺序
void InsertOrder(list<int>& OrderedList, int item)
{
    list<int>::iterator currIter = OrderedList.begin(), endIter = OrderedList.end();
    while ((currIter != endIter) && (*currIter<item))
    {
        currIter++;
    }
    OrderedList.insert(currIter, item);//前插
}

template <typename T>
void RemoveDuplicates(list<T>& alist)
{
    T currValue;
    typename list<T>::iterator currIter, tempIter;
    currIter = alist.begin();
    while (currIter != alist.end())
    {
        currValue = *currIter;
        tempIter = currIter;
        tempIter++;
        while(tempIter != alist.end())
        {
            if(*tempIter == currValue)
            {
                alist.erase(tempIter++);
            }
            else
            {
                tempIter++;
            }
        }
        currIter++;
    }
}

void listdemo()
{
    list<float> flist(8, 3.1415926);
    while (!flist.empty())
    {
        float f = flist.front();
        flist.pop_front();
        printf("%f", f);
    }
    printf("\n");
    

    string strArry[3] = {"array", "vector", "list"};
    list<string> strlist(strArry, strArry+3);
    printf("front :%s\tback :%s\n", strlist.front().c_str(), strlist.back().c_str());

#if 0
    list<CTestData> testdatelist;
    testdatelist.emplace(testdatelist.begin(), "default1");//直接在插入的位置构造一个实例，高效
    testdatelist.emplace_back("eb");//直接在插入的位置构造一个实例，高效
    testdatelist.push_back(CTestData("default2"));
    /* 以下两者无差别 */
    CTestData testdata1("test1"), testdata2("test2");
    testdatelist.emplace_back(testdata1);
    testdatelist.push_back(testdata2);
#endif

    list<char> charlist(8, 'a');
    printf("is palindrome %d\n", isPalindrome(charlist));
    charlist.push_back('b');
    printf("is palindrome %d\n", isPalindrome(charlist));
    charlist.push_front('b');
    printf("is palindrome %d\n", isPalindrome(charlist));
    
    list<char>::const_iterator citer = charlist.begin();//常量迭代器
    for (; citer != charlist.end(); citer++)
    {
        printf("%c", *citer);
    }
    printf("\n");






    ifstream fin;
    list<CWordFreq> wflist;
    list<CWordFreq>::iterator iter;
    string filename="../words.txt", word;
    fin.open(filename.c_str());
    if(!fin)
    {
        cerr<<"cannot open "<<filename<<endl;
        return ;
    }
    while (fin>>word)
    {
        CWordFreq wfitem(word);
        iter = CWordFreq::SeqSearch(wflist.begin(), wflist.end(), wfitem);
        if (wflist.end() != iter)
        {
            (*iter).Increment();
        }
        else
        {
            wflist.push_back(wfitem);
        }
    }
    Display(wflist);

    list<int> orderedlist{1,2,3,4,5,6,77,88};
    InsertOrder(orderedlist, 77);
    for (const int item : orderedlist)
    {
        printf("%d,", item);
    }
    printf("\n\n");

    RemoveDuplicates<int>(orderedlist);
    for (const int item : orderedlist)
    {
        printf("%d,", item);
    }
    printf("\n\n");
}


int main()
{
    listdemo();
    return 0;
}